package com.example.weixinsystem.security.handler;

import com.alibaba.fastjson2.JSON;
import com.weixin.common.core.constant.HttpStatus;
import com.weixin.common.core.domain.Result;
import com.weixin.common.core.utils.WebUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @className: AccessDeniedHandlerImpl
 * @description: 授权的异常处理
 * @author: zhangjunfa
 * @date: 2023/5/29 16:28
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        String json = JSON.toJSONString(Result.fail(HttpStatus.FORBIDDEN, "您的权限不足"));
        //处理移除
        WebUtil.renderString(response, json);
    }
}
